<?php


namespace app\common\validate;


class ProductValidate extends CommonValidate
{
    protected $rule    = [
        'id|ID'                                  => 'require|integer|between:1,100000000',
        'ids|商品ID'                               => 'require|enumIds',
        'product_id|商品ID'                        => 'require|integer|between:1,100000000',
        'category_id|商品分类ID'                     => 'require|integer|between:1,100000000',
        'attr_group_id|商品规格ID'                   => 'require|integer|between:1,100000000',
        'attr_group_category_id|商品分类ID'          => 'require|integer|between:1,100000000',
        'category_name|商品分类名称'                   => 'require|length:1,20',
        'attr_group_name|商品规格名称'                 => 'require|length:1,20',
        'product_name|商品名称'                      => 'require|length:1,45',
        'product_category_id|商品分类ID'             => 'require|integer|between:1,100000000',
        'product_logo_url|商品Logo图'               => 'require',
        'product_share_url|商品分享图'                => 'require',
        'product_wechat_share_url|商品微信分享图'       => 'require',
        'product_mini_share_url|商品小程序分享图'        => 'require',
        'product_image_urls|商品图'                 => 'require|enumArray',
        'product_desc|商品描述'                      => 'require|length:1,5000',
        'product_status|商品描述'                    => 'require|bool',
        'product_sku|商品SKU'                      => 'require|enumSku',
        'product_attr_info|商品规格信息'               => 'require|enumAttrInfo',
        'product_gift_points_proportion|赠送的积分比例' => 'require|integer',
        'product_tags|商品标签'                      => 'array',
    ];
    protected $message = [];
    protected $scene   = [
        'getProduct'            => [
            'getProduct',
        ],
        'getProductByIds'       => [
            'ids',
        ],
        'postProduct'           => [
            'product_name',
            'product_category_id',
            'product_logo_url',
            'product_share_url',
            'product_wechat_share_url',
            'product_mini_share_url',
            'product_image_urls',
            'product_desc',
            'product_status',
            'product_sku',
            'product_attr_info',
            'product_gift_points_proportion',
            'product_tags',
        ],
        'putProduct'            => [
            'product_id',
            'product_name',
            'product_category_id',
            'product_logo_url',
            'product_share_url',
            'product_wechat_share_url',
            'product_mini_share_url',
            'product_image_urls',
            'product_desc',
            'product_status',
            'product_sku',
            'product_attr_info',
            'product_gift_points_proportion',
            'product_tags',
        ],
        'deleteProduct'         => ['id'],
        'postProductCategory'   => [
            'category_name',
        ],
        'putProductCategory'    => [
            'category_id',
            'category_name',
        ],
        'deleteProductCategory' => ['category_id'],

        'postProductAttrGroup'   => [
            'attr_group_name',
            'attr_group_category_id',
        ],
        'putProductAttrGroup'    => [
            'attr_group_id',
            'attr_group_name',
            'attr_group_category_id',
        ],
        'deleteProductAttrGroup' => [
            'attr_group_id',
        ],
    ];

    /**
     * @param $value
     * @param $rule
     * @param $data
     * @return bool
     */
    protected function enumSku($value, $rule, $data): ?bool
    {
        try {
            //            "product_sku": [
            //                {
            //                    "sku_name": "20ml*绿色",
            //                    "price": "100",
            //                    "stock": "999",
            //                    "virtual_sales": "25",
            //                    "cost_price": "899"
            //                },
            //                ...
            //            ]
            foreach ($value as $sku) {
                if (!array_key_exists('sku_name', $sku) || !array_key_exists('price', $sku) || !array_key_exists('stock', $sku) || !array_key_exists('virtual_sales', $sku) || !array_key_exists('cost_price', $sku)) {
                    return false;
                }
                if (!(false !== filter_var($sku['price'], FILTER_VALIDATE_FLOAT)) || !(false !== filter_var($sku['cost_price'], FILTER_VALIDATE_FLOAT)) || !(false !== filter_var($sku['virtual_sales'], FILTER_VALIDATE_INT)) || !(false !== filter_var($sku['stock'], FILTER_VALIDATE_INT))) {
                    return false;
                }
            }
            return true;
        } catch (\Exception $e) {
            return false;
        }
    }

    /**
     * @param $value
     * @param $rule
     * @param $data
     * @return bool
     */
    protected function enumAttrInfo($value, $rule, $data): ?bool
    {
        try {
            //            "product_attr_info": [
            //                {
            //                    "attr_group_id": 100001,
            //                    "attr_names": [
            //                        "绿色",
            //                        "黄色"
            //                    ],
            //                },
            //                ...
            //            ]
            foreach ($value as $attr_info) {
                if (!array_key_exists('attr', $attr_info) || !array_key_exists('sku_names', $attr_info)) {
                    return false;
                }
                foreach ($attr_info['sku_names'] as $sku_name) {
                    if (!$sku_name) {
                        return false;
                    }
                }
            }
            return true;
        } catch (\Exception $e) {
            return false;
        }
    }

    public function enumUrls($v): ?bool
    {
        try {
            if (!is_array($v)) {
                return false;
            }
            foreach ($v as $item) {
                if (!(false !== filter_var($item, FILTER_VALIDATE_URL))) {
                    return false;
                }
            }
            return true;
        } catch (\Exception $e) {
            return false;
        }
    }
}